package com.tourism.home.service;

import com.tourism.home.utils.RandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class EmailService {
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    JavaMailSender jms;
    public void send(String email){
        //建立邮件消息
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        //发送者
        mainMessage.setFrom("15923623671@163.com");
        //接收者
        mainMessage.setTo(email);
        String yzcode = RandomCodeUtil.getRandomCode();
        HttpSession httpSession = httpServletRequest.getSession();
        httpSession.setAttribute("yzcode",yzcode);
        httpSession.setAttribute("email",email);
        String messge = "你离成为Ly论坛中的一员就差一步啦，快领取下方激活码吧! \n" + yzcode;
        //发送的标题
        mainMessage.setSubject("Ly论坛欢迎你！");
        //发送的内容
        mainMessage.setText(messge);
        jms.send(mainMessage);
    }

    public void forgetPassword(String email){
        //建立邮件消息
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        //发送者
        mainMessage.setFrom("15923623671@163.com");
        //接收者
        mainMessage.setTo(email);
        String yzcode = RandomCodeUtil.getRandomCode();
        HttpSession httpSession = httpServletRequest.getSession();
        httpSession.setAttribute("yzcode",yzcode);
        httpSession.setAttribute("email",email);
        String messge = "您好，您申请重置密码，当前正在执行重置密码验证，如非本人操作请勿泄露验证码。改密验证码： \n" + yzcode;
        //发送的标题
        mainMessage.setSubject("Ly重置密码验证！");
        //发送的内容
        mainMessage.setText(messge);
        jms.send(mainMessage);
    }

}
